/**
 * Created by zhr on 2017/07/12.
 */
import React from "react";
import Relay from "react-relay";
import { Table, message } from 'antd';
import ReactKey from "../../../util/ReactKey";
import Util from "../../../util/Util"
import ProgressDetailMutaion from './mutation/ProgressDetail';

import './css/style.css'
export default class table extends React.Component {
    constructor(){
        super();
        this.state={
            data: [],
            tableHead: [],
            pagination: {},
            loading: false,
        }
        this.enterpriseId = sessionStorage.getItem('enterpriseId')
    }

    componentWillMount() {
        /*给body动态添加类名*/
        let body = document.getElementsByTagName("body")[0];
        Util.addClass(body,"table_filter");
        this.getProgressDetailList()
    }
    getProgressDetailList() {
        const record = this.props.record;
        Relay.Store.commitUpdate(
            new ProgressDetailMutaion({
                enterpriseId: this.enterpriseId,
                orderId: record.orderId,
                styleNo: record.styleNo,
                productionType: this.props.type,
                groupIds: record.groupIds,
            }), {
                onSuccess: (res) => {
                    let data = res.detailProgress.detailProgress;
                    if(data.errorCode==0){
                        this.setState({
                            timeList: JSON.parse(data.productionProgressDetailModel.timeList),
                            mapList: JSON.parse(data.productionProgressDetailModel.mapList),
                            groupList: JSON.parse(data.productionProgressDetailModel.groupList)
                        })
                    } else if (data.errorCode == '300002'){

                    } else {
                        message.info(data.value)
                    }
                }
            }
        );
    }
    titleData(type){
        if (type == 1) {
            return <span>累计裁剪数：{this.props.record.countCut}</span>
        } else if (type == 2) {
            return <span>累计缝制数：{this.props.record.countTailor}</span>
        } else if (type == 3) {
            return <span>累计后整数：{this.props.record.countClear}</span>
        } else if (type == 4) {
            return <span>累计包装数：{this.props.record.countPack}</span>
        }
    }
    render() {
        const { timeList, mapList,groupList } = this.state;
        const type = this.props.type;
        let tableHead = [];
        timeList && tableHead.push(timeList['name']);
        timeList && tableHead.push(timeList['total']);
        for (var i in timeList) {
            if (timeList[i] == '班组' || timeList[i] == '累计') {

            } else {
                tableHead.push(timeList[i]);
            }
        }
        let groupFilter = [];
        groupList &&　groupList.map((v,i)=>{
            groupFilter.push({
                text: v.groupName,
                value: v.groupName,
            })
        })
        const columns = mapList && tableHead &&　tableHead.map((v,i,ele) =>{
              if ( i === 0) {
                    return (
                        {
                            title: tableHead[i],
                            dataIndex: tableHead[i],
                            key: tableHead[i],
                            filters: groupFilter,
                            // filteredValue: groupList.name || null,
                            onFilter: (value, record) =>{
                                // return record.styleNo.match(value);//模糊匹配
                                return record['班组'] == value;//精确匹配
                            } ,
                            filterMultiple:true,
                        }
                    )
              } else if ( i === 1) {
                  return (
                      {
                          title: tableHead[i],
                          dataIndex: tableHead[i],
                          key: tableHead[i],
                      }
                  )
              } else {
                  return (
                      {
                          title: Util.dayRate(Util.timeStamp(tableHead[i])),
                          dataIndex: tableHead[i],
                          key: tableHead[i],
                          render:(text,record)=>{
                              if (type == '2' && i >= 2 && text !== '-' && text < record.workGroupPlanAverageCount) {
                                  return <span style={{color:'red'}}>{text}</span>
                              } else {
                                  return <span>{text}</span>
                              }
                          }
                      }
                  )
              }

        })

        return (
            <div className="">
                {
                    tableHead && tableHead.length > 2 ? (
                        <Table columns={columns}
                               rowKey={ReactKey.key}
                               dataSource={mapList}
                               loading={this.props.loading}
                               className='order_table statistics_com_table progress_detail_table'
                               showHeader={true}
                               scroll={{x:800}}
                               bordered
                               title={()=>{
                                   return (
                                       <p className="progress_detail_title">
                                    <span>
                                        <span>订单编号：{this.props.record.orderId}</span>
                                    </span>
                                           <span>
                                        <span>款号：{this.props.record.styleNo}</span>
                                    </span>
                                           <span>
                                        <span>合同数量：{this.props.record.agreement}</span>
                                    </span>
                                           <span>
                                       {
                                           this.titleData(type)
                                       }
                                    </span>
                                       </p>
                                   )
                               }}
                        />
                    ) : (
                        <div className="proofing_progress_none">
                            <img src={require("../img/empty1.png")} alt=""/>
                            <br/>
                            <span>生产尚未开始</span>
                        </div>
                    )
                }
            </div>
        )
    }
}
